<template>
	<view class="container">
		<view class="input-content">
			<up-textarea v-model="value" placeholder="输入发帖内容" autoHeight border="none" height="20"></up-textarea>
		</view>

		<view class="upload-content">
			<up-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
				:previewFullImage="true" :maxCount="3"></up-upload>
		</view>
		<view class="button">
			<view class="button-content" @click="post">
				提交
			</view>
		</view>
		<up-toast ref="uToastRef"></up-toast>
	</view>
</template>

<script>
	import {
		forumPost
	} from '../../api/InternalCommunication.js'
	export default {
		data() {
			return {
				show: false,
				value: '',
				fileList1: [],
			};
		},
		methods: {
			post() {
				const images = this.fileList1.map(item => item.url)
				if (this.value == '') {
					this.$refs.uToastRef.show({
						type: 'error',
						icon: false,
						message: '请输入发帖内容'
					})
					return;
				}
				forumPost({
					content: this.value,
					images: images
				}).then((res) => {
					if (res.code == 2000) {
						this.$refs.uToastRef.show({
							type: 'success',
							icon: false,
							message: '发布成功'
						})
						setTimeout(() => {
							uni.navigateBack({
								url: './index'
							})
							uni.$emit('queryList');
						}, 2000)
					} else {
						this.$refs.uToastRef.show({
							type: 'error',
							icon: false,
							message: res.msg
						})
					}
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: JSON.parse(result).data.url
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: '/system/upload/file/', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							'Content-Type': 'multipart/form-data' // 根据需要设置请求头
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 25rpx;

		.input-content {
			height: auto;
			background-color: #fff;
		}

		.upload-content {
			background-color: #fff;
			margin-top: 25rpx;
			padding: 16rpx 0 0 16rpx;
		}

		.button {
			margin-top: 100rpx;
			display: flex;
			justify-content: center;

			.button-content {
				color: #fff;
				background-color: #eb8a1d;
				border-radius: 50rpx;
				width: 250rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				font-size: 30rpx;
			}
		}
	}
</style>